package com.apps.ftapp.activity;

import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.NoticeDetailModel;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class NoticeDetailActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.zuozhe)
    TextView zuozhe;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.activity_notice_detail)
    ScrollView activityNoticeDetail;

    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_detail);
        ButterKnife.bind(this);

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String id = bundle.getString("id");

        loadData(id);

        setTitle("消息详情");

        L.i("main", "::::" + id);

    }

    //要加内容需要整个修改，现在是直接加载的一段html代码
    private void loadData(String id) {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("id", id);
        Async.post("article/detail", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));
                    NoticeDetailModel result = gson.fromJson(new String(responseBody), NoticeDetailModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        title.setText(result.getMessage().getTitle());
                        time.setText("时间：" + result.getMessage().getTime());
//                        title.setText(result.getMessage().get(0).getTitle());
                        webView.loadData(result.getMessage().getBody(), "text/html; charset=UTF-8", null);

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));
            }
        });
    }
}
